#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <vector>

void demonstrate_quoted() {
  std::cout << "  C++14 std::quoted 演示:\n\n";

  // 1. 基本用法
  std::cout << "  1. 基本用法:\n";

  std::string text = "Hello World";
  std::cout << "    不使用quoted: " << text << "\n";
  std::cout << "    使用quoted:   " << std::quoted(text) << "\n";

  // 2. 空字符串
  std::cout << "\n  2. 空字符串:\n";

  std::string empty = "";
  std::cout << "    空字符串: " << std::quoted(empty) << "\n";

  // 3. 包含特殊字符的字符串
  std::cout << "\n  3. 包含特殊字符的字符串:\n";

  std::string special = "Say \"Hello\"";
  std::cout << "    包含引号: " << std::quoted(special) << "\n";

  std::string newline = "Line1\nLine2";
  std::cout << "    包含换行: " << std::quoted(newline) << "\n";

  // 4. 自定义分隔符
  std::cout << "\n  4. 自定义分隔符:\n";

  std::string name = "Alice";
  std::cout << "    默认引号: " << std::quoted(name) << "\n";
  std::cout << "    单引号:   " << std::quoted(name, '\'') << "\n";
  std::cout << "    括号:     " << std::quoted(name, '(', ')') << "\n";

  // 5. 与流操作结合
  std::cout << "\n  5. 与流操作结合:\n";

  std::ostringstream oss;
  oss << "Name: " << std::quoted("Bob") << ", Age: " << 30;
  std::cout << "    流输出: " << oss.str() << "\n";

  // 6. 从带引号的字符串读取
  std::cout << "\n  6. 从带引号的字符串读取:\n";

  std::istringstream iss1("\"Hello World\"");
  std::string read1;
  iss1 >> std::quoted(read1);
  std::cout << "    读取: " << read1 << "\n";

  std::istringstream iss2("'Single Quotes'");
  std::string read2;
  iss2 >> std::quoted(read2, '\'');
  std::cout << "    读取单引号: " << read2 << "\n";

  // 7. CSV格式处理示例
  std::cout << "\n  7. CSV格式处理示例:\n";

  std::vector<std::string> csv_fields = {"Name", "Age", "City"};
  std::cout << "    CSV行: ";
  for (size_t i = 0; i < csv_fields.size(); ++i) {
    std::cout << std::quoted(csv_fields[i]);
    if (i < csv_fields.size() - 1) {
      std::cout << ",";
    }
  }
  std::cout << "\n";

  // 8. 处理包含逗号的字段
  std::cout << "\n  8. 处理包含逗号的字段:\n";

  std::vector<std::string> csv_data = {
    "John Doe",
    "25",
    "New York, NY"
  };

  std::cout << "    CSV数据: ";
  for (size_t i = 0; i < csv_data.size(); ++i) {
    std::cout << std::quoted(csv_data[i]);
    if (i < csv_data.size() -1) {
      std::cout << ",";
    }
  }
  std::cout << "\n";

  // 9. JSON格式示例
  std::cout << "\n  9. JSON格式示例:\n";

  std::cout << "    JSON对象:\n";
  std::cout << "    {\n";
  std::cout << "      \"name\": " << std::quoted("Alice") << ",\n";
  std::cout << "      \"age\": 30,\n";
  std::cout << "      \"city\": " << std::quoted("Beijing") << "\n";
  std::cout << "    }\n";

  // 10. 处理转义字符
  std::cout << "\n  10. 处理转义字符:\n";

  std::string with_escape = "Path: C:\\Users\\Name";
  std::cout << "    带转义: " << std::quoted(with_escape) << "\n";

  // 11. 从输入流读取带引号的字符串
  std::cout << "\n  11. 从输入流读取:\n";

  std::istringstream iss3("First \"Second Word\" Third");
  std::string word1, word2, word3;
  iss3 >> word1 >> std::quoted(word2) >> word3;
  std::cout << "    单词1: " << word1 << "\n";
  std::cout << "    单词2: " << word2 << "\n";
  std::cout << "    单词3: " << word3 << "\n";

  // 12. 优势说明
  std::cout << "\n  std::quoted 的优势:\n";
  std::cout << "  - 自动处理引号和转义字符\n";
  std::cout << "  - 简化CSV/JSON等格式的处理\n";
  std::cout << "  - 类型安全，避免手动字符串操作\n";
  std::cout << "  - 支持自定义分隔符\n";
  std::cout << "  - 输入输出双向支持\n";
}

